<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" />
          {{ t('导出') }}
        </a-button>
        <a-button type="default" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" />
          {{ t('导入') }}
        </a-button>
        <a-button type="primary" @click="handleForm({})" v-auth="'cardd:jbgz:carddJbgz:edit'">
          <Icon icon="i-fluent:add-12-filled" />
          {{ t('新增') }}
        </a-button>
        <a-button
          type="primary"
          @click="handleDistribution({})"
          v-auth="'cardd:jbgz:carddJbgz:edit'"
        >
          <Icon icon="i-fluent:add-12-filled" />
          {{ t('发放') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.jbgzRymc }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <DistributionForm @register="registerDistributionDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsCarddJbgzCarddJbgzList">
  import { unref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicColumn, BasicTable, useTable } from '/@/components/Table';
  import {
    carddJbgzDelete,
    carddJbgzDisable,
    carddJbgzEnable,
    carddJbgzListData,
  } from '/@/api/cardd/jbgz/carddJbgz';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';
  import DistributionForm from './distributionForm.vue';

  const { t } = useI18n('cardd.jbgz.carddJbgz');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('基本工资管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('人员名称'),
        field: 'jbgzRymc',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('人员名称'),
      dataIndex: 'jbgzRymc',
      key: 'a.jbgz_rymc',
      sorter: true,
      width: 90,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('基本工资'),
      dataIndex: 'jbgzJbgz',
      key: 'a.jbgz_jbgz',
      sorter: true,
      width: 90,
      align: 'right',
    },
    {
      title: t('缴费基数'),
      dataIndex: 'jbgzJfjs',
      key: 'a.jbgz_jfjs',
      sorter: true,
      width: 90,
      align: 'right',
    },
    {
      title: t('养老保险个人'),
      dataIndex: 'jbgzYlbxgryj',
      key: 'a.jbgz_ylbxgryj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('养老保险单位'),
      dataIndex: 'jbgzYlbxdwyj',
      key: 'a.jbgz_ylbxdwyj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('养老保险小计'),
      dataIndex: 'jbgzYlbxxj',
      key: 'a.jbgz_ylbxxj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('失业险个人'),
      dataIndex: 'jbgzSyxgryj',
      key: 'a.jbgz_syxgryj',
      sorter: true,
      width: 105,
      align: 'right',
    },
    {
      title: t('失业险单位'),
      dataIndex: 'jbgzSyxdwyj',
      key: 'a.jbgz_syxdwyj',
      sorter: true,
      width: 105,
      align: 'right',
    },
    {
      title: t('失业险小计'),
      dataIndex: 'jbgzSyxxj',
      key: 'a.jbgz_syxxj',
      sorter: true,
      width: 105,
      align: 'right',
    },
    {
      title: t('工伤保险个人'),
      dataIndex: 'jbgzGsbxgryj',
      key: 'a.jbgz_gsbxgryj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('工伤保险单位'),
      dataIndex: 'jbgzGsbxdwyj',
      key: 'a.jbgz_gsbxdwyj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('工伤保险小计'),
      dataIndex: 'jbgzGsbxxj',
      key: 'a.jbgz_gsbxxj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('基本医疗个人'),
      dataIndex: 'jbgzJbylgryj',
      key: 'a.jbgz_jbylgryj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('基本医疗单位'),
      dataIndex: 'jbgzJbyldwyj',
      key: 'a.jbgz_jbyldwyj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('基本医疗小计'),
      dataIndex: 'jbgzJbylxj',
      key: 'a.jbgz_jbylxj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('大额医疗补助个人'),
      dataIndex: 'jbgzDeylbzgryj',
      key: 'a.jbgz_deylbzgryj',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('大额医疗补助单位'),
      dataIndex: 'jbgzDeylbzdwyj',
      key: 'a.jbgz_deylbzdwyj',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('大额医疗补助小计'),
      dataIndex: 'jbgzDeylbzxj',
      key: 'a.jbgz_deylbzxj',
      sorter: true,
      width: 130,
      align: 'right',
    },
    {
      title: t('生育保险个人'),
      dataIndex: 'jbgzSybxgryj',
      key: 'a.jbgz_sybxgryj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('生育保险单位'),
      dataIndex: 'jbgzSybxdwyj',
      key: 'a.jbgz_sybxdwyj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('生育保险小计'),
      dataIndex: 'jbgzSybxxj',
      key: 'a.jbgz_sybxxj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('个人应缴合计'),
      dataIndex: 'jbgzGryjhj',
      key: 'a.jbgz_gryjhj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('单位应缴合计'),
      dataIndex: 'jbgzDwyjhj',
      key: 'a.jbgz_dwyjhj',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('全勤奖'),
      dataIndex: 'jbgzQqj',
      key: 'a.jbgz_qqj',
      sorter: true,
      width: 90,
      align: 'right',
    },
    {
      title: t('电话补贴'),
      dataIndex: 'jbgzDhbt',
      key: 'a.jbgz_dhbt',
      sorter: true,
      width: 90,
      align: 'right',
    },
    {
      title: t('交通补贴'),
      dataIndex: 'jbgzJtbt',
      key: 'a.jbgz_jtbt',
      sorter: true,
      width: 90,
      align: 'right',
    },
    {
      title: t('洗车补贴'),
      dataIndex: 'jbgzXcbt',
      key: 'a.jbgz_xcbt',
      sorter: true,
      width: 90,
      align: 'right',
    },
    {
      title: t('无违章投诉补贴'),
      dataIndex: 'jbgzWwztsbt',
      key: 'a.jbgz_wwztsbt',
      sorter: true,
      width: 120,
      align: 'right',
    },
    {
      title: t('合计'),
      dataIndex: 'jbgzHj',
      key: 'a.jbgz_hj',
      sorter: true,
      width: 70,
      align: 'right',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 120,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑基本工资'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'cardd:jbgz:carddJbgz:edit',
      },
      {
        icon: 'i-ant-design:stop-outlined',
        color: 'error',
        title: t('停用基本工资'),
        popConfirm: {
          title: t('是否确认停用基本工资'),
          confirm: handleDisable.bind(this, record),
        },
        auth: 'cardd:jbgz:carddJbgz:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'i-ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用基本工资'),
        popConfirm: {
          title: t('是否确认启用基本工资'),
          confirm: handleEnable.bind(this, record),
        },
        auth: 'cardd:jbgz:carddJbgz:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除基本工资'),
        popConfirm: {
          title: t('是否确认删除基本工资'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'cardd:jbgz:carddJbgz:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDistributionDrawer, { openDrawer: openDistributionDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: carddJbgzListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/cardd/jbgz/carddJbgz/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  function handleDistribution(record: Recordable) {
    const selKeys = getSelectRowKeys();
    if (selKeys.length === 0) {
      showMessage(t('请选择要发放基本工资的人员'));
      return;
    }
    record.selIds = selKeys.join(',');
    openDistributionDrawer(true, record);
  }

  async function handleDisable(record: Recordable) {
    const params = { id: record.id };
    const res = await carddJbgzDisable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleEnable(record: Recordable) {
    const params = { id: record.id };
    const res = await carddJbgzEnable(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await carddJbgzDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }
</script>
